I may be being overly paranoid about string table pollution, but "just-in-case", here's a function to avoid the issue (if it exists).
Buffer ::+= (Buffer b, int x)
{
char c[10] = {'0','1','2','3','4','5','6','7','8','9'}
char d[10] = {'0','0','0','0','0','0','0','0','0','0'}
bool neg = (x<0)
if (neg) x=-x
int i
for ( i=0; i<10; i++ )
{
d[i] = c[ x % 10 ]
x = x / 10
if (x==0) break
}
if (neg) b += '-'
for ( i=i; i>=0; i-- ) b += d[i]
return b
}
Let me know if (a) you can prove that converting an integer to string via (MyInt "") does not pollute the string table, or (b) you have a better avoidance algorithm. Best regards Alex AlexTidmarsh - Sun Nov 09 01:55:51 EST 2014 |
Re: Non-polluting Integer to Buffer Seems to me at the very best such an algorithm can only reduce at the max half wastage of the string table. Putting "12" and "23" into the buffer with result in 4 bytes when you take it out; the only question is whether you waste another 4 bytes putting it in. Seems to me integers are not a big waste of string table space. In any event, using your array "d" seems like a waste and limits your integers to 10 digits; I think you want a single loop that does this:
-Louie |
Re: Non-polluting Integer to Buffer llandale - Thu Nov 13 16:32:23 EST 2014 Seems to me at the very best such an algorithm can only reduce at the max half wastage of the string table. Putting "12" and "23" into the buffer with result in 4 bytes when you take it out; the only question is whether you waste another 4 bytes putting it in. Seems to me integers are not a big waste of string table space. In any event, using your array "d" seems like a waste and limits your integers to 10 digits; I think you want a single loop that does this:
-Louie The limit on digits is not really a problem, since any integer using more digits won't fit in an integer anyway. I chose a 10-digit limit, but even that would overflow the output integer and cause numeric wrap. However, I take your point about the arrays - since putting them inside the routine probably means they are created afresh each time. Maybe this would be better if either (a) they were declared in the outer scope or (b) they were deleted inside the routine after use.
|
Re: Non-polluting Integer to Buffer AlexTidmarsh - Tue Nov 18 09:00:31 EST 2014 The limit on digits is not really a problem, since any integer using more digits won't fit in an integer anyway. I chose a 10-digit limit, but even that would overflow the output integer and cause numeric wrap. However, I take your point about the arrays - since putting them inside the routine probably means they are created afresh each time. Maybe this would be better if either (a) they were declared in the outer scope or (b) they were deleted inside the routine after use.
Oh - hang about! The 'c' and 'd' are not delete-able arrays anyway! They are just stack variables, and thus (in most languages) should not consume any space after the call (not even the string table, because they are character variables, not strings). I have however edited the routine in one important aspect:
//--------------------------------
Buffer &::+= (Buffer &b, int x)
//--------------------------------
// NB: Requires named Buffers only - can't be called with slices or inline create.
{
char c[10] = {'0','1','2','3','4','5','6','7','8','9'}
char d[10] = {'0','0','0','0','0','0','0','0','0','0'}
bool neg = (x<0)
if (neg) x=-x
int i
for ( i=0; i<10; i++ )
{
d[i] = c[ x % 10 ]
x = x / 10
if (x==0) break
}
if (neg) b += '-'
for ( i=i; i>=0; i-- ) b += d[i]
return b
}
|
Re: Non-polluting Integer to Buffer AlexTidmarsh - Tue Nov 18 09:39:40 EST 2014 Oh - hang about! The 'c' and 'd' are not delete-able arrays anyway! They are just stack variables, and thus (in most languages) should not consume any space after the call (not even the string table, because they are character variables, not strings). I have however edited the routine in one important aspect:
//--------------------------------
Buffer &::+= (Buffer &b, int x)
//--------------------------------
// NB: Requires named Buffers only - can't be called with slices or inline create.
{
char c[10] = {'0','1','2','3','4','5','6','7','8','9'}
char d[10] = {'0','0','0','0','0','0','0','0','0','0'}
bool neg = (x<0)
if (neg) x=-x
int i
for ( i=0; i<10; i++ )
{
d[i] = c[ x % 10 ]
x = x / 10
if (x==0) break
}
if (neg) b += '-'
for ( i=i; i>=0; i-- ) b += d[i]
return b
}
I basically agree with Louie, however since the digits need to be reordered I would suggest something like this:
Buffer ::+= (Buffer b, int x)
{
if (x<0) { b+= '-'; x = -x; }
int comp = 0
if (x < 10) { comp = 1 }
else if (x < 100) { comp = 10 }
else if (x < 1000) { comp = 100 }
else if (x < 10000) { comp = 1000 }
else if (x < 100000) { comp = 10000 }
else if (x < 1000000) { comp = 100000 }
else if (x < 10000000) { comp = 1000000 }
else if (x < 100000000) { comp = 10000000 }
else if (x < 1000000000) { comp = 100000000 }
else { comp = 1000000000 }
while (comp > 0) { b += cg[x / comp]; x %= comp; comp /= 10; }
return b
}
which saves you the reordering and is about 25%-30% faster than the original (see attached profiling). Regards, Mathias Attachments profiling.dxl |
Re: Non-polluting Integer to Buffer Mathias Mamsch - Wed Nov 19 09:20:25 EST 2014 I basically agree with Louie, however since the digits need to be reordered I would suggest something like this:
Buffer ::+= (Buffer b, int x)
{
if (x<0) { b+= '-'; x = -x; }
int comp = 0
if (x < 10) { comp = 1 }
else if (x < 100) { comp = 10 }
else if (x < 1000) { comp = 100 }
else if (x < 10000) { comp = 1000 }
else if (x < 100000) { comp = 10000 }
else if (x < 1000000) { comp = 100000 }
else if (x < 10000000) { comp = 1000000 }
else if (x < 100000000) { comp = 10000000 }
else if (x < 1000000000) { comp = 100000000 }
else { comp = 1000000000 }
while (comp > 0) { b += cg[x / comp]; x %= comp; comp /= 10; }
return b
}
which saves you the reordering and is about 25%-30% faster than the original (see attached profiling). Regards, Mathias Cool.
|